<template>
  <div class="app-container">
    <el-card>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small">
          <el-form-item label="筛选条件：">
            <el-input placeholder="请输入筛选条件" v-model="listQuery.conditionsName" maxlength="20" clearable>
            </el-input>
          </el-form-item>
          <el-form-item label="条件分组：">
            <el-select v-model="listQuery.groupId" placeholder="全部" clearable class="input-width">
              <el-option v-for="item, index in categoryOptions" :key="index" :label="item.label"
                :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-refresh" style="" @click="handleResetSearch()" size="small">重置</el-button>
            <el-button icon="el-icon-search" type="primary" @click="handleSearchList()" size="small">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button @click="addDialog()" size="mini" class="btn-add" type="primary" icon="el-icon-plus"
        >新建条件及选项</el-button>
        <el-button v-btnpermission="$btnPer.ResourceBtnRules.cateResource" size="small" class="btn-add"
          @click="handleShowCategory()">资源分类</el-button>
      </div>
      <div class="table-container">
        <el-table ref="resourceTable" :data="list" style="width: 100%" v-loading="listLoading" border>
          <el-table-column label="序号" width="100" align="center">
            <template slot-scope="scope">{{ scope.row.id }}</template>
          </el-table-column>
          <el-table-column label="条件名称" align="center">
            <template slot-scope="scope">{{ scope.row.conditionsName }}</template>
          </el-table-column>
          <el-table-column label="是否为必要条件" align="center">
            <template slot-scope="scope">{{ scope.row.must==1?"是":"否" }}</template>
          </el-table-column>
          <el-table-column label="创建人" align="center">
            <template slot-scope="scope">{{ scope.row.createBy }}</template>
          </el-table-column>
          <el-table-column label="创建时间" width="160" align="center">
            <template slot-scope="scope">{{
              scope.row.createTime
            }}</template>
          </el-table-column>
          <el-table-column label="操作" width="140" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="handleUpdate(scope.$index, scope.row)">
                修改
              </el-button>
              <el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
          layout="total, sizes,prev, pager, next,jumper" :current-page.sync="listQuery.pageNum"
          :page-size="listQuery.pageSize" :page-sizes="[10, 15, 20]" :total="total">
        </el-pagination>
      </div>
      <addDialog v-if="addDialogDisplay" ref="addDialogDiy" ></addDialog>
      <modifyDialog @getList="getList" v-if="modifyDialogDisplay" ref="modifyDialog" ></modifyDialog>
    </el-card>
  </div>
</template>
<script>
import {
  fetchList,
  createResource,
  updateResource,
  deleteResource
} from "@/api/condition";
import addDialog from "./addDialog.vue";
import modifyDialog from "./modifyDialog.vue";
import { listAllCate } from "@/api/resourceCategory";
import { getDictData, dictTypeFunList } from "@/utils/dict";
import { formatDate } from "@/utils/date";


const defaultResource = {
  id: null,
  name: null,
  url: null,
  categoryId: null,
  description: ""
};
export default {
  name: "resourceList",
  components:{
    addDialog,
    modifyDialog
  },
  data() {
    return {
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        conditionsName: '',
        groupId: '',
      },
      list: null,
      total: null,
      listLoading: false,
      resource: Object.assign({}, defaultResource),
      isEdit: false,
      categoryOptions: [],
      defaultCategoryId: null,
      rules: {
        name: [{ required: true, message: "请输入资源名称", trigger: "blur" }],
        url: [{ required: true, message: "请输入资源路径", trigger: "blur" }]
      },
      addDialogDisplay:false,
      modifyDialogDisplay:false,
      // modifyData:{}
    };
  },
  created() {
    this.getList();
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    }
  },
  methods: {
    addDialog(){
      this.addDialogDisplay=true
      setTimeout(()=>{
        this.$refs.addDialogDiy.openlog()
      },100)
    },
    handleUpdate(index, row) {
      this.modifyDialogDisplay=true
      // this.modifyData = row;
      setTimeout(()=>{
        this.$refs.modifyDialog.openlog()
        this.$refs.modifyDialog.getProductData(row.conditions)
      },100)
    },
    handleResetSearch() {
      this.listQuery = {
        pageNum: 1,
        pageSize: 10,
        conditionsName: '',
        groupId: '',
      }
      this.getList();
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleDelete(index, row) {
      this.$confirm("是否要删除该资源?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteResource(row.conditions).then(response => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.getList();
        });
      });
    },
    // handleDialogConfirm(formName) {
    //   this.$refs[formName].validate(valid => {
    //     if (valid) {
    //       this.$confirm("是否要确认?", "提示", {
    //         confirmButtonText: "确定",
    //         cancelButtonText: "取消",
    //         type: "warning"
    //       }).then(() => {
    //         if (this.isEdit) {
    //           updateResource(this.resource.id, this.resource).then(response => {
    //             this.$message({
    //               message: "修改成功！",
    //               type: "success"
    //             });
    //             this.dialogVisible = false;
    //             this.getList();
    //           });
    //         } else {
    //           createResource(this.resource).then(response => {
    //             this.$message({
    //               message: "添加成功！",
    //               type: "success"
    //             });
    //             this.dialogVisible = false;
    //             this.getList();
    //           });
    //         }
    //       });
    //     } else {
    //       // console.log("error submit!!");
    //       return false;
    //     }
    //   });
    //   return;
    // },
    // handleShowCategory() {
    //   this.$router.push({ path: "/ums/resourceCategory" });
    // },
    getList() {
      this.listLoading = true;
      //获取页面列表
        fetchList(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        });
      //获取条件分组
      getDictData("conditions_group").then((res) => {
        this.categoryOptions = res
      });

    },
  }
};
</script>
<style scoped>
.app-container {
  height: calc(100vh - 50px);
  background-color: #f5f5f5;
  padding: 20px;
}
</style>